package com.ba.base.view.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * 瀑布流列表分割线
 *
 * @param space 指定宽度
 */
class StaggeredItemDecoration(private val space: Int = 1) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        when (val p = parent.getChildAdapterPosition(view)) {
            0 -> {
                outRect.left = space
                outRect.right = space / 2
                outRect.bottom = space / 2
                outRect.top = space
            }
            1 -> {
                outRect.left = space / 2
                outRect.right = space
                outRect.bottom = space / 2
                outRect.top = space
            }
            else -> {
                when (p % 2) {
                    0 -> {
                        outRect.left = space
                        outRect.right = space / 2
                        outRect.bottom = space / 2
                        outRect.top = space / 2
                    }
                    1 -> {
                        outRect.left = space / 2
                        outRect.right = space
                        outRect.bottom = space / 2
                        outRect.top = space / 2
                    }
                }
            }
        }
    }

}